package com.bianguo.android.iocdemo.view.widget.state;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.view.animation.LinearInterpolator;

public class RotateState extends SplashState {

    public RotateState(int animTime) {
        ValueAnimator animator = ValueAnimator.ofFloat(0, (float) (2 * Math.PI));
        animator.setDuration(animTime);
        animator.setRepeatCount(2);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(valueAnimator -> {
            if (stateListener != null) {
                stateListener.updateCurrentAngle((float) valueAnimator.getAnimatedValue());
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (stateListener != null) {
                    stateListener.onAnimEnd(RotateState.this);
                }
            }
        });
        animator.start();
    }

    @Override
    public void drawState(Canvas canvas) {
        if (stateListener != null) {
            stateListener.onDrawState(this, canvas);
        }
    }
}
